(function(){
	//控件统一管理对象
	Ccay.Control = $.IAdapter({
		mapping:{
			//渲染对象
			render:function(target,field,value){
				value != null && this.setValue(target,field,value);
				return target;
			},
			//根据字段获取对象值
			getValue:function(target,field){
				return field.renderer.getValue(target,field.format);
			},
			//根据字段设置对象值
			setValue:function(target,field,value){
				field.renderer.render(target,value,field.format);
			}
		},
		proto:{
			//初始化Field
			initField:function(field){
				//初始化渲染器
				if(!field.renderer || typeof(field.renderer) == "string")
					field.renderer = Ccay.UI.Renderer.get(field.renderer || field.type);
					
				field.renderer.initFormat && field.renderer.initFormat(field.format);
				
				//初始化编辑器
				if(!field.editor || typeof(field.editor) == "string")
					field.editor = Ccay.Editor.get(field.editor || field.type);
				field.editorOp = $.extend(true,{
		            type: field.type,
		            field: field.field,
		            format: field.format,
		            rule : field.rule
		        }, field.editorOp);
		        return field;
			},
			//渲染字段
			render:function(type,container,field,value,ns){
				var ctr = this.get(type).render(container,field,value);
				field.attrs && ctr.attr(field.attrs);
				ctr.addClass("ccay-control").attr("rtype",type);
				field.onRender && field.onRender(ctr,field,type);
								
				ns && ctr.attr('mapping',ns);
				return ctr;
			},
			//生成控件的通用标签
			buildCtrTag:function(field,renderType){
				return '<span class=\'ccay-field\' mapping=\'{0}\' rtype=\'{1}\' />'.formatText(field,renderType || "");
			},
			//将区域内所有的通用控件标签编译成控件
			build:function(panel,modal,renderType,validOp,clearTag,fnHandler){
				var rule = {}, 
					//生成验证规则方法
					fnBuildRule = function(ctr,rType,field,modal){
						if(rType != 'view')
						{
							var fieldRule = field.editorOp && field.editorOp.rule ? field.editorOp.rule : null;
							if(fieldRule || field.rule)
								fieldRule = $.extend(true,{},field.rule,fieldRule);
							
							fieldRule && (rule[field.field] = fieldRule);
						}
						fnHandler && fnHandler(ctr,rType,field,modal);
					};
				//变量控件标签进行编译
				panel.find('span.ccay-field').each(function(){
					Ccay.Control.buildCtr($(this),modal,renderType,clearTag,fnBuildRule);
				});
				//进行验证初始化
				if (!$.isEmptyObject(rule) || (validOp &&validOp.rule))
				{
					if(validOp) 
						$.extend(validOp,{fields:rule});
					else
						validOp = {fields:rule};
						
//					(panel[0].tagName == "FORM" ?  panel : panel.find("form.init"))
						//panel.ccayValidate(validOp).removeClass("init");
				}
				panel.ccayValidate(validOp).removeClass("init");
			},
			//将标签编译成控件
			buildCtr:function(tag,modal,renderType,clearTag,fnHandler){
				var rType = tag.attr('rtype') || renderType,
					ns = tag.attr('mapping'),
					field = Ccay.DataHelper.getObj(ns,modal,true),
					ctr = this.render(rType,tag,field,modal.attr(field.field),ns);
					fnHandler && fnHandler(ctr,rType,field,modal);
	
					if(clearTag)
					{
						tag.children().insertAfter(tag);
						tag.remove();
					}
					else
						tag.removeAttr("rtype").removeAttr("mapping");
			},
			//设置区域整体值
			setValue:function(panel,modal,data,type){
				this.eachControl(panel,modal,type,function(ctr,field,name,renderCtr){
					var value = Ccay.DataHelper.getObj(name,data);
					renderCtr.setValue(ctr,field,value);
				});
			},
			//获取区域整体值
			getValue:function(panel,modal,type){
				//type || (type = "edit");
				var data = {};
				this.eachControl(panel,modal,type,function(ctr,field,name,renderCtr){
					var value = renderCtr.getValue(ctr,field);
					value != null && value !== "" && Ccay.DataHelper.setObj(name,data,value);
				});
				return data;
			},
			//遍历区域内所有内置control
			eachControl:function(panel,modal,type,fnHandler){
				var filter = ".ccay-control",renderCtr = Ccay.Control.get(type);
				type && (filter += "[rtype='" + type  + "']")
				
				panel.find(filter).each(function(){
					var ctr = $(this),
						name = ctr.attr('name'),
						renderType = ctr.attr("rtype"),
						field = modal.getField(ctr.attr("mapping"));
						
					field && fnHandler(ctr,field,name,(type == renderType ? renderCtr : Ccay.Control.get(renderType)));
				})
			},
			//区域验证
			valid:function(panel){
				if(panel.valid()){
					var result = true;
					//对复合控件单独验证
					panel.find('.control-composite').each(function(){
						if(!$(this).manager().valid())
						{
							result = false;
							return false;
						}
					})
					return result;
				}
				return false;
			},
			ctrValue:function(ctr,modal,value){
				var ns = ctr.attr('mapping'),type = ctr.attr('rtype'),
					renderCtr = Ccay.Control.get(type),
					field = modal.getField(ns);
					
				if(field)
				{
					if(arguments.length > 2)
						renderCtr.setValue(ctr,field,value);
					else
						return renderCtr.getValue(ctr,field);
				}
				return this;
			}
		},
		setDefault:true
	});
	
	//注册编辑类控件基类
	Ccay.Control.add("edit",{
		//渲染方法
		render:function(container,field,value){
		    var ctr = field.editor.render(container,field.editorOp);
		    value != null && this.setValue(ctr,field,value);
		    return ctr;
		},
		//获取值方法
		getValue:function(target,field)
		{
			if(field.getValue)
				return field.getValue(target);
			else
				return this.parseValue(field.editor.getValue(target),field);
		},
		//设置值方法
		setValue:function(target,field,value){
			if(field.setValue)
				field.setValue(target,value);
			else
				field.editor.setValue(target,this.parseValue(value,field));
			return this;
		},
		//数据转换方法
		parseValue:function(value,field){
			if(value != null)
			{
				return field.renderer.parse(value.value || value,field.format);
			}
			return value;
		}
	});
})();